<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

	public $data 	= 	array();
 
	public function __construct() {
 
		//parent::Controller();
		parent::__construct(); //for CI 2.x users
 
		$this->load->module_model('content', 'news_model', 'content');
		$this->load->library('basic_library');
		$this->load->helper('text');
		$this->system_library->get_site_info();
	}
 
	public function index() 
	{
		
		/*$this->load->library('sitemaps');
		$posts = $this->store->get_id_category_sitemap();
		var_dump($posts);
		foreach($posts AS $post)
        {
            $item = array(
                "loc" => site_url("/store/product/category/" . $post->slug),
                // ISO 8601 format - date("c") requires PHP5
                "lastmod" => date("c", strtotime($post->last_modified)),
                "changefreq" => "hourly",
                "priority" => "0.8"
            );
            var_dump($item);
            $this->sitemaps->add_item($item);
        }

        $file_name = $this->sitemaps->build("sitemap_blog.xml");
        $reponses = $this->sitemaps->ping(site_url($file_name));
        redirect(site_url($file_name));*/
		
		$data['ItemID'] = 'Content';
		$data['title'] = $this->system_library->settings['site_name'];
		$data['description'] = $this->system_library->settings['site_description'];
 		$data['categories'] = $this->content->get_all_category();
		$this->_template['page']	= 'news/frontpage';
		$this->system_library->load($this->_template['page'], $data);
 
	}
	
	public function category($alias = null)
	{
		$id  = $this->basic_library->get_category_id($alias);
		$this->load->library('pagination');
		$config = array();
		$config['base_url'] = base_url().'chuyen-muc/'.$alias;
		$config['total_rows'] = $this->content->count_all_content($id);
		$config['per_page'] = 10;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3):0;
		$data['categoryid'] =	$id;
		$data['ItemID'] = 'Category';
		
		$data['blognews'] = $this->content->get_content_of_category($alias,$config['per_page'], $page);
		//var_dump($data['blognews']);
		
		foreach($data['blognews'] as $meta)
		{
			$title = $meta['name'];
			$description = $meta['catdescription'];
		}
		
		if($data['blognews'] == null)
		{
			$data['title'] = 'Nội dung đang cập nhật';
			$data['description'] = 'Nội dung đang cập nhật';
		}
		else
		{
			$data['title'] = $title;
			//$data['description'] = character_limiter($data['blognews'] ['catdescription'], 160);
			$data['description'] = $description;
		}
		
		$data['links'] = $this->pagination->create_links();
		$this->_template['page']	= 'news/category';
		$this->system_library->load($this->_template['page'], $data);
	}
	public function article($alias = null, $id =  null)
	{
		//echo $id; exit();
		$data['ItemID'] = 'ArticleDetail';		
		//$data['relateditems']	=	$this->content->get_related_items($id);
		$url=base_url().$alias.'-'.$id;
		$this->dangkyhoc($id,$url);
		$data['productinfo'] = $this->content->get_article_content($alias);
		$data['title'] = $data['productinfo']['title'];
		$catid = $data['productinfo']['category_id'];
		$data['description'] = character_limiter($data['productinfo']['description'], 160);
		$data['relatedcontent'] 	=	$this->content->get_related_content($catid, $alias);
		//var_dump($data['relatedcontent']);
		$this->_template['page']	= 'news/article';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function pages($alias = null)
	{
		$data['ItemID'] = 'PageDetail';
		$data['pages'] = $this->content->get_page($alias);
		$data['title'] = $data['pages']['title'];
		$data['description'] = character_limiter($data['pages']['description'], 160);
		$this->_template['page']	= 'news/page';
		$this->system_library->load($this->_template['page'], $data);
	}  
	
	public function dangkyhoc($id, $url)
	{
		
		$this->form_validation->set_rules('tenhv', 'Họ và tên', 'required|max_length[150]|xss_clean');
		$this->form_validation->set_rules('diachi', 'Địa chỉ', 'required|max_length[200]|xss_clean');
		$this->form_validation->set_rules('didong', 'Di động', 'required|numeric|xss_clean');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$datasend = array(
			'id_nganh'  =>  $id,
			'tennganh'	=>	$this->input->post('tennganh'),
			'tenhv'		=>	$this->input->post('tenhv'),
			'diachi'	=>	$this->input->post('diachi'),
			'didong'	=>	$this->input->post('didong'),
		);
		
		if($this->form_validation->run() == TRUE)
		{
			$this->content->dangkyhoc($id);
			$this->sendmail($datasend);
			$this->session->set_flashdata('message', 'Email xác nhận đã được gửi về Phòng đào tạo, chúng tôi sẽ liên hệ với bạn trong thời gian sớm nhất!');
			redirect($url, 'refresh');
		}
	}
	
	public function sendmail($datasend)
	{

		//config sendmail	
		$this->load->library('email');
		
		//use for gmail
		$config['protocol']    = 'smtp';
        $config['smtp_host']    = 'ssl://smtp.gmail.com';
        $config['smtp_port']    = '465';
        $config['smtp_timeout'] = '7';
        $config['smtp_user']    = 'ledancuoi@gmail.com'; // địa chỉ gmail
        $config['smtp_pass']    = 'qhe7211lad'; // pass gmail
        $config['charset']    = 'utf-8';
        $config['newline']    = "\r\n";
        $config['mailtype'] = 'html'; // or html
        $config['validation'] = TRUE; // bool whether to validate email or not
        $config['wordwrap'] = FALSE;      
        $this->email->initialize($config);
        
        //send mail
        $this->email->from($this->system_library->settings['admin_email'], $this->system_library->settings['site_name']);
        //$this->email->to($datasend['email']);
        $this->email->to($this->system_library->settings['admin_email']);
        $this->email->subject('Thông tin Đăng ký - Trường Trung Cấp Nghề Số 19 - BQP');
        
        $data['content'] = $datasend;
        //load view email template
        $msg = $this->load->view('sendmail', $data, true);
		$this->email->message($msg);  
        $this->email->send();
        
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/product.php */